<?php

namespace App\Games;

use App\Handlers\ApiHelper;
use App\Models\Enum\MailType;
use App\Handlers\CsvUploadHandler;
use App\Models\Enum\RechargeType;

class JianLingShiJie implements BaseGame
{
    private $_secretKey = '878ce0a222bcca2901468659d86adeba';

    //发送邮件
    public function sendMail($mail)
    {
        $uri = "http://{$mail->channel->server_ip}:{$mail->channel->port}/xiyou/gmtoolwriter";

        if ($mail->mail_type == MailType::SINGLE_SERVICE || $mail->mail_type == MailType::FULL_SERVICE) {
            $player = 'gopher';
            $cmdstr = 'globalmail ';
        } else if ($mail->mail_type == MailType::SINGLE) {
            $player = $mail->nickname;
            $cmdstr = 'mail ';
        } else {
            flash('邮件类型不支持')->error()->important();
            return false;
        }

        $serverid = $mail->server_id;
        if ($mail->mail_type == MailType::FULL_SERVICE) {
            $serverid = 'all';
        }

        //拼接发送的道具
        $itemStr = '';
        for ($i = 1; $i <= 5; $i++) {
            $item_type = $mail["item_type_{$i}"];
            $item_id = $mail["item_id_{$i}"];
            $item_num = $mail["item_num_{$i}"];
            if ($item_id >= 0 && $item_num > 0) {
                $itemStr .= "{$item_type}-{$item_id}-{$item_num},";
            }
        }
        if (!empty($itemStr)) {
            $itemStr = ' ' . substr($itemStr, 0, -1);
        }

        $content = str_replace('%5Cn', "\n", $mail->content);
        $cmdstr .= " {$mail->title} {$content}{$itemStr}";
        $sign = md5($cmdstr . $player . $serverid . $this->_secretKey);

        $params = [
            'cmdstr' => $cmdstr,
            'player' => $player,
            'serverid' => $serverid,
            'sign' => $sign,
        ];

        $result = ApiHelper::post($params, $uri);

        $result = json_decode($result, true);
        if ($result['code'] === 1) {
            return true;
        } else {
            return false;
        }
    }

    public function sendBatchMail($mail)
    {
        $uri = "http://{$mail->channel->server_ip}:{$mail->channel->port}/xiyou/gmtoolmultiwriter";

        $csvUploadHandler = new CsvUploadHandler();
        $datas = $csvUploadHandler->read(public_path() . $mail->attachment);;

        $command = [];

        $content = str_replace('%5Cn', "\n", $mail->content);

        foreach ($datas as $data) {
            $command[] = [
                'serverid' => $data[1],
                'player' => $data[0],
                'cmdstr' => "mail {$mail->title} {$content} {$data[2]}"
            ];
        }

        $sign = md5(json_encode($command) . $this->_secretKey);

        $params = [
            'commands' => $command,
            'sign' => $sign,
        ];

        $result = ApiHelper::post($params, $uri);

        $resultJson = json_decode($result, true);
        if (isset($resultJson['code']) && $resultJson['code'] == 1) {
            return true;
        } else {
            flash($result)->error()->important();
            return false;
        }
    }

    public function getUserInfoList($request, $channel)
    {
        $uri = "http://{$channel->server_ip}:{$channel->port}/gmtool/playerinfolist";

        $command = [
            //测试
//            'channel_id' => '',
//            'server_id' => 'test',
            //正式
            'channel_id' => $request->channel_id,
            'server_id' => $request->server_id,
        ];
        if (isset($request->uid) && !empty($request->uid)) {
            $command['uid'] = $request->uid;
        }
        if (isset($request->nickname) && !empty($request->nickname)) {
            $command['nickname'] = urlencode($request->nickname);
        }
        if (isset($request->page_size) && !empty($request->page_size)) {
            $command['page_size'] = strval($request->page_size);
        }
        if (isset($request->page) && !empty($request->page)) {
            $command['page_index'] = strval($request->page);
        }
        ksort($command);

        $sign = md5(json_encode($command) . $this->_secretKey);

        $params = [
            'commands' => $command,
            'sign' => $sign,
        ];

        $result = ApiHelper::post($params, $uri);

        $resultJson = json_decode($result, true);
        if (isset($resultJson['code']) && $resultJson['code'] == 1) {
            return $resultJson;
        } else {
            flash('接口错误:' . $result)->error()->important();
            return [];
        }
    }

    public function getUserInfoDetail($request, $channel)
    {
        $uri = "http://{$channel->server_ip}:{$channel->port}/gmtool/playerinfo";

        $command = [
            //测试
//            'channel_id' => '',
//            'server_id' => 'test',
            //正式
            'channel_id' => $request->channel_id,
            'server_id' => $request->server_id,
            'uid' => $request->uid,
        ];
        ksort($command);

        $sign = md5(json_encode($command) . $this->_secretKey);

        $params = [
            'commands' => $command,
            'sign' => $sign,
        ];

        $result = ApiHelper::post($params, $uri);

        $resultJson = json_decode($result, true);
        if (isset($resultJson['code']) && $resultJson['code'] == 1) {
            return $resultJson['data'];
        } else {
            flash($result)->error()->important();
            return [];
        }
    }

    //发送充值补偿
    public function sendRecharge($recharge)
    {
        $uri = "http://{$recharge->channel->server_ip}:{$recharge->channel->port}/xiyou/gmtoolwriter";

        $cmdstr = 'recharge ' . $recharge->item_id;

        if ($recharge->recharge_type == RechargeType::BATCH) {
            $serverid = 'all';
            $player = 'gopher';
        } else if ($recharge->recharge_type == RechargeType::SINGLE) {
            $serverid = $recharge->server_id;
            $player = $recharge->nickname;
        } else {
            flash('邮件类型不支持')->error()->important();
            return false;
        }

        $sign = md5($cmdstr . $player . $serverid . $this->_secretKey);

        $params = [
            'cmdstr' => $cmdstr,
            'player' => $player,
            'serverid' => $serverid,
            'sign' => $sign,
        ];

        $result = ApiHelper::post($params, $uri);

        $result = json_decode($result, true);
        if ($result['code'] === 1) {
            return true;
        } else {
            return false;
        }
    }

    public function sendBatchRecharge($recharge)
    {
        $uri = "http://{$recharge->channel->server_ip}:{$recharge->channel->port}/xiyou/gmtoolmultiwriter";

        $csvUploadHandler = new CsvUploadHandler();
        $datas = $csvUploadHandler->read(public_path() . $recharge->attachment);;

        $command = [];

        $content = str_replace('%5Cn', "\n", $recharge->content);

        foreach ($datas as $data) {
            $command[] = [
                'serverid' => $data[1],
                'player' => $data[0],
                'cmdstr' => "recharge {$data[2]}"
            ];
        }

        $sign = md5(json_encode($command) . $this->_secretKey);

        $params = [
            'commands' => $command,
            'sign' => $sign,
        ];

        $result = ApiHelper::post($params, $uri);

        $resultJson = json_decode($result, true);
        if (isset($resultJson['code']) && $resultJson['code'] == 1) {
            return true;
        } else {
            flash($result)->error()->important();
            return false;
        }
    }
}
